#include "controlwidget.h"

controlWidget::controlWidget(QWidget *parent) : QLabel(parent)
{

}

void controlWidget::mouseReleaseEvent(QMouseEvent *ev)
{
    emit signal_onClicked();
}

void controlWidget::paintEvent(QPaintEvent *ev)
{
    QLabel::paintEvent(ev);  //调用父类的paintevent来显示背景
    int width = this->width();
    int height = this->height();
    int side = qMin(width,height);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2,height / 2);
    painter.scale(side / 200.0,side / 200.0);
    Fun_drawArc(&painter);
}

void controlWidget::Fun_drawArc(QPainter *painter)
{
    int radius = 90;

//    double currentAngle = 360 * (secValue / 60.0);  //不能使用int类型来除，会直接取0

    painter->save();  //保存当前的状态
    painter->setBrush(Qt::NoBrush);

    QPen pen;
    pen.setWidth(18);
    pen.setCapStyle(Qt::RoundCap);  //轮廓线端点样式：圆形

    QRectF rect = QRectF(-radius,-radius,radius*2,radius*2); //矩形中心点的坐标为（0，0）

    pen.setColor(QColor("#E2F0D9"));  //轨道背景色
    painter->setPen(pen);
    painter->drawArc(rect,0 * 16,360 * 16); //角度被分成了16分之一。

    pen.setColor(QColor("#bacf65"));
    painter->setPen(pen);
    painter->drawArc(rect,90 * 16, currentPercentage * 360 * 16);

    painter->restore();  //恢复之前保存的状态
}

//改变当前的百分比值
void controlWidget::slot_onChangeCurrentPercentage(double currentPercentage)
{
    this->currentPercentage = 1 - currentPercentage;
    this->update();
}
